package com.apes.hr.base.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.recruit.model.RecruitChannel;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：人才储备库
 * @author Linwz
 * @create 2019-03-08 13:31
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TalentPool.CONST_TABLE_NAME)
public class TalentPool extends Model {
    public static final String CONST_TABLE_NAME = "hr_talent_pool";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "人才储备库", padding = 8)
    @Comment("人员编码")
    private String id; //人员编码

    @Name
    @Column(name = "name", nullable = false, length = 200)
    @Comment("人员名称")
    private String name; //人员名称

    @OneToOne
    @JoinColumn(name = "employee_id", referencedColumnName = "id")
    @Comment("人员档案")
    private Employee employee; //人员档案


    @Column(name = "state", length = 10)
    @Comment("状态")
    private String state; //状态： ('sent', '创建'), ('approve', '审核')

    @Column(name = "mobile_number", length = 16,unique = true)
    @Comment("手机号")
    private String mobileNumber; //手机号
//
//    @Column(name = "office_phone", length = 16)
//    @Comment("办公电话")
//    private String officePhone; //办公电话

    @Column(name = "telephone", length = 16)
    @Comment("固定电话")
    private String telephone; //固定电话

    @Column(name = "mail_address")
    @Comment("邮寄地址")
    private String mailAddress; //邮寄地址

    @Column(name = "postal_address")
    @Comment("通讯地址")
    private String postalAddress;  //通讯地址

    @Column(name = "id_card_address")
    @Comment("身份证地址")
    private String idCardAddress;  //身份证地址

//    @Column(name = "postal_code", length = 8)
//    @Comment("值班岗位")
//    private String postalCode; //邮政编码（改成值班岗位）

    @Column(name = "qq", length = 16)
    @Comment("qq")
    private String qqNumber; //qq

    @Column(name = "email", length = 32)
    @Comment("email")
    private String email; //email

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sex", referencedColumnName = "id")
    @Comment("性别")
    private Enumeration sex; //性别

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "birthday")
    @Comment("生日")
    private Date birthday; //生日

    @Column(name = "id_card", length = 32,unique = true)
    @Comment("身份证号")
    private String idCard;  //身份证号

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "id_card_term")
    @Comment("身份证到期时间")
    private Date idCardTerm;//身份证到期时间

    @Column(name = "weight", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("体重")
    private double weight; //体重

    @Column(name = "height", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("身高")
    private double height; //身高


    @Column(name = "native_place",length = 40)
    @Comment("籍贯")
    private String nativePlace; //籍贯

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "census_reg_nature", referencedColumnName = "id")
    @Comment("户口性质")
    private Enumeration censusRegNature; //户口性质

    //户籍地址
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "reg_country_id", referencedColumnName = "id")
//    @Comment("户籍地址.国家")
//    private Country regCountry; //户籍地址.国家
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "reg_province_id", referencedColumnName = "id")
//    @Comment("户籍地址.省份")
//    private Area regProvince; //户籍地址.省份
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "reg_city_id", referencedColumnName = "id")
//    @Comment("户籍地址.城市")
//    private Area regCity; //户籍地址.城市
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "reg_county_id", referencedColumnName = "id")
//    @Comment("户籍地址.区县")
//    private Area regCounty; //户籍地址.区县
//
//    @Column(name = "reg_address", length = 500)
//    @Comment("户籍地址.地址")
//    private String regAddress; //户籍地址.地址
//
//    //居住地址
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "living_country_id", referencedColumnName = "id")
//    @Comment("居住地址.国家")
//    private Country livingCountry; //居住地址.国家
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "living_province", referencedColumnName = "id")
//    @Comment("居住地址.省份")
//    private Area livingProvince; //居住地址.省份
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "living_city_id", referencedColumnName = "id")
//    @Comment("居住地址.城市")
//    private Area livingCity; //居住地址.城市
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "living_county_id", referencedColumnName = "id")
//    @Comment("居住地址.区县")
//    private Area livingCounty; //居住地址.区县
//
//    @Column(name = "living_address", length = 500)
//    @Comment("居住地址.地址")
//    private String livingAddress; //居住地址.地址

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "nation", referencedColumnName = "id")
    @Comment("民族")
    private Enumeration nation; //民族

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "blood_type", referencedColumnName = "id")
    @Comment("血型")
    private Enumeration bloodType; //血型

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "political_visage", referencedColumnName = "id")
    @Comment("政治面貌")
    private Enumeration politicalVisage; //政治面貌

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "marital_state", referencedColumnName = "id")
    @Comment("婚姻状况")
    private Enumeration maritalState; //婚姻状况


    @Column(name = "the_only_child", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否独生子女")
    private boolean theOnlyChild = true; //是否独生子女

    @Column(name = "contact", length = 64)
    @Comment("紧急联系人")
    private String contact;  //紧急联系人

    @Column(name = "contact_phone", length = 64)
    @Comment("紧急联系人电话")
    private String contactPhone;  //紧急联系人电话

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "mandarin_Level", referencedColumnName = "id")
    @Comment("普通话水平")
    private Enumeration mandarinLevel;  //普通话水平(skillProficiency)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "local_dialect_Level", referencedColumnName = "id")
    @Comment("本地方言水平")
    private Enumeration localDialectLevel;  //本地方言水平(skillProficiency)

    @Column(name = "typing_speed", nullable = false, columnDefinition = "number(8,0) default 0")
    @Comment("打字速度")
    private int typingSpeed = 0;    //打字速度

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ime", referencedColumnName = "id")
    @Comment("输入法")
    private Enumeration ime;    //输入法

//    //---------------------------------------------------------
//    // @NotNull
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "company_id", referencedColumnName = "id")
//    @Comment("所属公司")
//    private PartyRole company; //所属公司
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "work_branch_id", referencedColumnName = "id")
//    @Comment("工作部门")
//    private Branch workBranch; //工作部门
//
//    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
//    @Temporal(TemporalType.DATE)
//    @Column(name = "entry_date")
//    @Comment("入职日期")
//    private Date entryDate;  //入职日期
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "post_id", referencedColumnName = "id")
//    @Comment("岗位")
//    private WorkPost post; //岗位
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "job_id", referencedColumnName = "id")
//    @Comment("职位")
//    private Job job; //职位
//
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "rank_id", referencedColumnName = "id")
//    @Comment("职级")
//    private Rank rank; //职级
//
//    @Column(name = "post_describe",length = 120)
//    @Comment("岗位描述")
//    private String postDescribe;//岗位描述
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "employee_state_id", referencedColumnName = "id")
//    @Comment("员工状态")
//    private Enumeration employeeState; //员工状态(employeeState)从申请面试开始  同旧框架 :(000088:在职;000089:已离职;000417:待入职)
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "on_duty_state", referencedColumnName = "id")
//    @Comment("在职状态")
//    private Enumeration onDutyState; //在职状态(onDutyState)
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "quit_state_id", referencedColumnName = "id")
//    @Comment("离职状态")
//    private Enumeration quitState; //离职状态(quitState) 000412	预离职；000413	正常离职；000414	未交接离职
//
//    @Column(name = "staff_share",  nullable = false, columnDefinition = "number(16,4) default 0")
//    @Comment("份数")
//    private double staffShare = 0; //份数 份额
//
    @Column(name = "soldier",  nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否退役军人")
    private boolean soldier = false; // 是否是否退役军人(0:否;1:是)

    @Column(name = "driver",  nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否有驾驶证")
    private boolean driver = false; // 是否有驾驶证(0:否;1:是)

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "driver_term")
    @Comment("驾驶证到期时间")
    private Date driverTerm;//驾驶证到期时间
//
//    @Column(name = "echelon", nullable = false, columnDefinition = "number(1,0) default 0")
//    @Comment("是否梯队")
//    private boolean echelon = false;  //是否梯队(0:否;1:是)\

    @Column(name = "skilled", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否熟练驾驶")
    private boolean skilled = false;//是否熟练驾驶（0：否；1：是）

//    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
//    @Comment("有效")
//    private boolean valid = true; //有效

    @OneToMany(mappedBy = "talentPool", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("家庭成员")
    private Set<EmployeeFamily> employeeFamilies = new HashSet(); //家庭成员

    public Set<EmployeeFamily> getEmployeeFamilies() {
        if (employeeFamilies == null) return null;
        for (EmployeeFamily employeeFamily : employeeFamilies) {
            employeeFamily.setTalentPool(this);
        }
        return employeeFamilies;
    }


    @OneToMany(mappedBy = "talentPool", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("教育背景")
    private Set<EmployeeEducation> employeeEducations = new HashSet(); //教育背景

    public Set<EmployeeEducation> getEmployeeEducations() {
        if (employeeEducations == null) return null;
        for (EmployeeEducation employeeEducation : employeeEducations) {
            employeeEducation.setTalentPool(this);
        }
        return employeeEducations;
    }

    @OneToMany(mappedBy = "talentPool", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("工作经历")
    @OrderBy("startDate desc")
    private Set<EmployeeWorkHistory> employeeWorkHistories = new HashSet(); //工作经历

    public Set<EmployeeWorkHistory> getEmployeeWorkHistories() {
        if (employeeWorkHistories == null) return null;
        for (EmployeeWorkHistory employeeWorkHistory : employeeWorkHistories) {
            employeeWorkHistory.setTalentPool(this);
        }
        return employeeWorkHistories;
    }

    // 技能 证书
    @OneToMany(mappedBy="talentPool", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("技能 证书")
    private Set<EmployeeSkillCertificate> employeeSkillCertificates = new HashSet<>();
    public Set<EmployeeSkillCertificate> getEmployeeSkillCertificates() {
        if (employeeSkillCertificates == null) return null;
        for (EmployeeSkillCertificate employeeSkillCertificate : employeeSkillCertificates) {
            employeeSkillCertificate.setTalentPool(this);
        }
        return employeeSkillCertificates;
    }


    @Column(name="english_level")
    @Comment("英语水平")
    private String englishLevel;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "driver_license_type", referencedColumnName = "id")
    @Comment("驾照类型")
    private Enumeration driverLicenseType;//驾照类型

    @Column(name = "name_before")
    @Comment("曾用名")
    private String nameBefore;//曾用名

//    @Column(name="drive_proficiency")
//    @Comment("驾驶熟练程度")
//    private  String driveProficiency; //驾驶熟练程度

    @Transient
    private Set<Enumeration> getReason(String source) {
        if (StringUtil.isEmpty(source)) return null;
        Set<Enumeration> persons = new HashSet<>();
        String[] rows = source.split(";");
        for (String person : rows) {
            if (person.contains("|")) {
                persons.add(SpringManager.getBean(EnumerationRepository.class).findOne(StringUtils.split(person, "|")[0]));
            }
        }
        return persons;
    }

    private String toConvertReason(Set<Enumeration> persons) {
        StringBuffer sb = new StringBuffer();
        for (Enumeration enumeration : persons) {
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(enumeration.getId());
            sb.append("|");
            sb.append(enumeration.getDescription());  // 格式 id|description;
        }
        return sb.toString();
    }

    //
    @Column(name = "age",length = 3)
    @Comment("年龄")
    private Integer age =0; //年龄


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fertility", referencedColumnName = "id")
    @Comment("是否生育")
    private Enumeration fertility ;//生育状况

    @Column(name = "contact_relation", length = 200)
    @Comment("和紧急联系人关系")
    private String contactRelation;  //和紧急联系人关系

    @Column(name = "title", length = 200)
    @Comment("职称")
    private String title;  //从业资格证、职称

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("应聘门店")
    private Dept dept;  //应聘门店

    @Column(name = "application_job")
    @Comment("应聘职位")
    private String applicationJob;  //应聘职位

    @Column(name = "base_pay", length = 64)
    @Comment("期望底薪")
    private String basePay;  //期望底薪

    @Column(name = "month_income", length = 64)
    @Comment("期望月收入")
    private String monthIncome;  //期望月收入

    @Column(name = "development")
    @Comment("发展规划")
    private String development;  //发展规划

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "availability")
    @Comment("可到岗时间")
    private Date availability;  //可到岗时间

    @Column(name = "live_dormitory", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("需要住员工宿舍")
    private boolean liveDormitory =false;  //需要住员工宿舍

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recruit_channel_id", referencedColumnName = "id")
    @Comment("招聘渠道")
    private RecruitChannel recruitChannel;  //招聘渠道

    @Column(name = "nature_of_job")
    @Comment("职位性质")
    private String natureOfJob;  //职位性质

    @Column(name = "statement", length = 64)
    @Comment("声明内容")
    private String statement;  //声明内容

    @Column(name = "same_trade_keep_out", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("竞争行业禁止协议")
    private boolean sameTradeKeepOut = false; //是否有签《竞争行业禁止协议》

    @Column(name = "has_other_job", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否与其他单位存在劳动关系")
    private boolean hasOtherJob = false;  //是否与其他单位存在劳动关系

    @Column(name = "cognitive_Employee_id")
    @Comment("认识的员工")
    private String cognitiveEmployee;   //推荐人

    @Column(name = "cognitive_relation", length = 16)
    @Comment("与该员工的关系")
    private String cognitiveRelation;  //与推荐人的关系

    @Column(name = "choose_social_security")
    @Comment("购买社保意向")
    private String chooseSocialSecurity;    //购买社保意向

    public void setchooseSocialSecurity(String chooseSocialSecurity) {
    }

    public void chooseSocialSecurityToApp(String chooseSocialSecurity) {
        this.chooseSocialSecurity = chooseSocialSecurity;
    }

    @Transient
    private Set<Enumeration> chooseSocialSecuritySet;

    public Set<Enumeration> getchooseSocialSecuritySet() {
        if (isWrite() || isLoad()) {
            chooseSocialSecuritySet = getReason(this.chooseSocialSecurity);
        }
        return chooseSocialSecuritySet;
    }

    public void setchooseSocialSecuritySet(Set<Enumeration> chooseSocialSecuritySet) {
        this.chooseSocialSecuritySet = chooseSocialSecuritySet;
        if (!isWrite()) {
            this.chooseSocialSecurity = toConvertReason(this.chooseSocialSecuritySet);
        }
    }


    @Column(name = "office", length = 4000)
    @Comment("办公软件")
    private String office;  //办公软件

    public void setOffice(String office) {
    }
    public void OfficeToApp(String office) {
        this.office = office;
    }

    @Transient
    private Set<Enumeration> officeSet;

    public Set<Enumeration> getofficeSet() {
        if (isWrite() || isLoad()) {
            officeSet = getReason(this.office);
        }
        return officeSet;
    }
    public void setofficeSet(Set<Enumeration> officeSet) {
        this.officeSet = officeSet;
        if (!isWrite()) {
            this.office = toConvertReason(this.officeSet);
        }
    }
    @Column(name="different_works" , length = 4000)
    @Comment("可接受的异地工作地点")
    private String differentWorks;  //可接受的异地工作地点

    public void setDifferentWorks(String differentWorks) {
    }
    public void DifferentWorksToApp(String differentWorks) {
        this.differentWorks = differentWorks;
    }

    @Transient
    private Set<Enumeration> differentWorksSet;

    public Set<Enumeration> getDifferentWorksSet() {
        if (isWrite() || isLoad()) {
            differentWorksSet = getReason(this.differentWorks);
        }
        return differentWorksSet;
    }
    public void setDifferentWorksSet(Set<Enumeration> differentWorksSet) {
        this.differentWorksSet = differentWorksSet;
        if (!isWrite()) {
            this.differentWorks = toConvertReason(this.differentWorksSet);
        }
    }
    @Column(name = "design_software", length = 4000)
    @Comment("设计软件")
    private String designSoftware; //设计软件
    public void setDesignSoftware(String designSoftware) {

    }
    public void DesignSoftwareToApp(String designSoftware) {
        this.designSoftware = designSoftware;
    }
    @Transient
    private Set<Enumeration> designSoftwareSet;

    public Set<Enumeration> getdesignSoftwareSet() {
        if (isWrite() || isLoad()) {
            designSoftwareSet = getReason(this.designSoftware);
        }
        return designSoftwareSet;
    }
    public void setdesignSoftwareSet(Set<Enumeration> designSoftwareSet) {
        this.designSoftwareSet = designSoftwareSet;
        if (!isWrite()) {
            this.designSoftware = toConvertReason(this.designSoftwareSet);
        }
    }

    @Column(name = "functions")
    @Comment("常用函数")
    private String functions;    //常用函数

    public void setFunctions(String functions) {
    }
    public void FunctionsToApp(String Functions) {
        this.functions = Functions;
    }
    @Transient
    private Set<Enumeration> functionSet;

    public Set<Enumeration> getfunctionSet() {
        if (isWrite() || isLoad()) {
            functionSet = getReason(this.functions);
        }
        return functionSet;
    }

    public void setfunctionSet(Set<Enumeration> functionSet) {
        this.functionSet = functionSet;
        if (!isWrite()) {
            this.functions = toConvertReason(this.functionSet);
        }
    }

    @Column(name = "unacceptable_id", length = 4000)
    @Comment("不能接受的企业行为")
    private String unacceptables;  //不能接受的企业行为

    public void setunacceptables(String unacceptables) {
    }
    public void unacceptablesToApp(String unacceptables) {
        this.unacceptables = unacceptables;
    }
    @Transient
    private Set<Enumeration> unacceptableSet;

    public Set<Enumeration> getunacceptableSet() {
        if (isWrite() || isLoad()) {
            unacceptableSet = getReason(this.unacceptables);
        }
        return unacceptableSet;
    }

    public void setunacceptableSet(Set<Enumeration> unacceptableSet) {
        this.unacceptableSet = unacceptableSet;
        if (!isWrite()) {
            this.unacceptables = toConvertReason(this.unacceptableSet);
        }
    }

    @Column(name = "apply_job", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否已入职")
    private boolean applyJob = false; //是否已入职

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "print_date")
    @Comment("打印时间")
    private Date printDate;//打印时间

    @Column(name = "print_qty")
    @Comment("打印次数")
    private int printQty = 0;//打印次数

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "commit_date")
    @Comment("提交时间")
    private Date commitDate;

    @Column(name = "commit_qty")
    @Comment("提交次数")
    private int commitQty = 0;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "annex", columnDefinition = "CLOB")
    private String annex;

}
